package com.maxlen.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maxlen.admin.entity.Role;
import com.maxlen.admin.entity.dto.PageParamDto;
import com.maxlen.admin.entity.vo.PermissionVo;
import com.maxlen.admin.entity.vo.RolePermissionVo;
import com.maxlen.admin.service.IRoleService;
import com.maxlen.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 分页排序搜索查询所有列表
     * @param pageParamDto
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody PageParamDto pageParamDto){
        Page<Role> list = roleService.findAllByPage(pageParamDto);
        return R.ok().put("data", list);
    }

    /**
     * 添加角色
     * @param rolePermissionVo
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody RolePermissionVo rolePermissionVo){
        if(roleService.add(rolePermissionVo)){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Long id){
        if(roleService.deleteById(id)){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量删除
     * @param multipleId
     * @return
     */
    @PostMapping("/deleteMultiple")
    public R delete(@RequestBody List<Long> multipleId){
        roleService.deleteByIdList(multipleId);
        return R.ok();
    }

    /**
     * 更新角色
     * @param rolePermissionVo
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody RolePermissionVo rolePermissionVo){
        if(roleService.updateById(rolePermissionVo)){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 根据 role_id 获取权限信息
     * @param id
     * @return
     */
    @GetMapping("/getPermissionByRoleId/{id}")
    public R getPermissionByRoleId(@PathVariable Long id){
        List<PermissionVo> permissionVoList = roleService.findPermissionByRoleId(id);
        return R.ok().put("data", permissionVoList);
    }

}
